/*
#
#  Copyright 2011 Matthew Jannace
#
#  This file is part of RoboCode BlackHoleAgent
#
#  RoboCode BlackHoleAgent is free software: you can redistribute it and/or
#  modify it under the terms of the GNU General Public License as
#  published by the Free Software Foundation, either version 3 of the
#  License, or (at your option) any later version.
#
#  RoboCode BlackHoleAgent is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#  General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with RoboCode BlackHoleAgent. If not, see
#  <http://www.gnu.org/licenses/>.
#
 */
package com.jannaceltd.TeamBlackHoleBeer.Modules.CustomEvents;

import com.jannaceltd.TeamBlackHoleBeer.BlackHole_Abstract;
import com.jannaceltd.TeamBlackHoleBeer.BlackHole_Interface;
import com.jannaceltd.TeamBlackHoleBeer.Modules.HelperClasses.VirtualBullets;
import java.awt.geom.Point2D;
import robocode.Condition;

/**
 * Custom Event Condition to Test if a Bullet Distance Buffer Condition is Violated
 * @author Matthew Jannace <mjannace@gmail.com>
 * @version 1.0
 */
public class Bullet_DistanceBufferCondition_Violated extends Condition implements BlackHole_Interface {

    private BlackHole_Abstract Agent;
    private Point2D.Double myPos;
    private long CurrentTime;

    /**
     * Creates A Custom Event Condition to Test if a Bullet Distance Buffer Condition is Violated
     * @param Agent
     * @see CustomEventProcessor
     */
    public Bullet_DistanceBufferCondition_Violated(BlackHole_Abstract Agent) {
        this.Agent = Agent;
    }

    /**
     * Test if the Bullet Distance Buffer Condition is Violated
     * @return True if Bullet Distance Buffer Condition is IS Violated else False
     * @see CustomEventProcessor
     */
    @Override
    public boolean test() {
        myPos = Agent.getPos();
        CurrentTime = Agent.getTime() + 5;

        if (BulletDistanceViolated()) {
            return true;
        }
        return false;
    }

    /**
     * Checks if any Friendly Fire is less than the
     * BULLET_DISTANCE_BUFFER_VIOLATED range
     * @return True if Bullet Distance Buffer Condition IS Violated else False
     * @see #test()
     */
    public boolean BulletDistanceViolated() {
        for (VirtualBullets bullet : Agent.friendlyFire.values()) {
            if ((myPos.distance(bullet.getBulletPosition(CurrentTime))) <= BULLET_DISTANCE_BUFFER_VIOLATED) {
                if (!Agent.EventProcessor.Violator.containsKey(bullet.getName())) {
                    Agent.EventProcessor.Violator.put(bullet.getName(), CurrentTime);
                    return true;
                }
            }
        }
        return false;
    }
}
